<?php
/**
* Created by PhpStorm.
* User: simon
* Date: 14-2-26
* Time: 下午3:06
* commonFunc.php 常用的公共辅助函数库
*/

if ( ! function_exists('app'))
{
    /**
     *
     * @param  string  $make
     * @return mixed
     */
    function app($attr = null)
    {
        if ( ! is_null($attr))
        {
            return app()->$attr;
        }

        return Ulue::$app;
    }
}

if ( ! function_exists('service'))
{
    /**
     *
     * @param  string  $make
     * @return mixed
     */
    function service($name)
    {
        if ( $name )
        {
            return Ioc::get($name);
        }

        return null;
    }
}


if ( ! function_exists('config'))
{
    /**
     *
     * @param  string  $make
     * @return mixed
     */
    function config($key = null)
    {
        if ( ! is_null($key))
        {
            return Ioc::get('app.config')->get($key);
        }

        return Ioc::get('app.config');
    }
}

if (function_exists('get_classname')) {
    /**
     * get classname by full classname
     * @param string $class
     * @return null|string
     */
    function get_classname( $class )
    {
        return parse_full_classname( $class, false);
    }
}

if (function_exists('get_namespace'))
{
    /**
     * get classname by full classname
     * @param $class
     * @return null|string
     */
    function get_namespace( $class )
    {
        return parse_full_classname( $class, true);
    }
}

/**
 * parse_full_classname 解析类名
 * @param  mixed $class 类 或 类的实例
 * @param  boolean $onlyName true:namespace false:classname
 * @return null|string
 */
function parse_full_classname( $class, $onlyName = true)
{
    if (is_object($class)) {
        $classname = get_class($class);
    } else if ( $class && is_string($class) ) {
        $classname = rtrim($class,'\\ ');
    } else {
        return null;
    }

    $classname = trim($classname);

    return $onlyName ? basename($classname) : trim(dirname($classname),'.');
}

if (function_exists('str_cut'))
{
    function str_cut($string,$start,$length='')
    {
        if ($length=='') {
            $length = $start;
            $start  = 0;
        }

        if (strlen($string)>$length) {
            return substr($string, $start,$length).'...';
        }

        return $string;
    }
}

if ( ! function_exists('value'))
{
    /**
     * Return the default value of the given value.
     *
     * @param  mixed  $value
     * @return mixed
     */
    function value($value)
    {
        return $value instanceof Closure ? $value() : $value;
    }
}

if ( ! function_exists('with'))
{
    /**
     * Return the given object. Useful for chaining.
     *
     * @param  mixed  $object
     * @return mixed
     */
    function with($object)
    {
        return $object;
    }
}

// 字符截断输出
function truncate($string,$start,$length='')
{
    if ($length=='') {
        $length = $start;
        $start  = 0;
    }

    if (strlen($string)>$length) {
        return substr($string, $start,$length).'...';
    }

    return $string;
}



/**
 * 外部链接增加nofllow $content 文章内容 $domain 当前网站域名
 * 注意！过滤的域名需要是最后不带“/”的 ex : http://xx.yy.com
 */
function data_add_nofollow($content,$domain){
    preg_match_all('/href="(.*?)"/',$content,$matches);
    if ($matches){
        foreach($matches[1] as $val){
            if ( strpos($val,$domain)===false ) $content=str_replace('href="'.$val.'"', 'href="'.$val.'" rel="external nofollow" ',$content);
        }
    }
    preg_match_all('/src="(.*?)"/',$content,$matches);
    if ($matches){
        foreach($matches[1] as $val){
            if ( strpos($val,$domain)===false ) $content=str_replace('src="'.$val.'"', 'src="'.$val.'" rel="external nofollow" ',$content);
        }
    }
    return $content;
}
